package com.linewow.xhyy.nihaonew.activity.account;

import android.content.Intent;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.linewow.xhyy.nihaonew.R;
import com.linewow.xhyy.nihaonew.application.NiHaoApp;
import com.linewow.xhyy.nihaonew.base.BaseActivity;
import com.linewow.xhyy.nihaonew.base.SimpleBaseEntity;
import com.linewow.xhyy.nihaonew.entity.db.AppInfo;
import com.linewow.xhyy.nihaonew.db.NiHaoHelper;
import com.linewow.xhyy.nihaonew.event.MessageEvent;
import com.linewow.xhyy.nihaonew.model.AppConstants;
import com.linewow.xhyy.nihaonew.net.api.NiHaoOkgo;

import org.simple.eventbus.EventBus;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * Created by LXR on 2016/9/20.
 */
public class LoginActivity extends BaseActivity {


    @Bind(R.id.toolbar_back_title)
    TextView toolbarBackTitle;
    @Bind(R.id.toolbar_back_left)
    ImageView toolbarBackLeft;
    @Bind(R.id.login_email_edit)
    EditText loginEmailEdit;
    @Bind(R.id.login_pwd_edit)
    EditText loginPwdEdit;
    @Bind(R.id.login_btn)
    RelativeLayout loginBtn;
    @Bind(R.id.login_forget_pwd)
    TextView loginForgetPwd;
    @Bind(R.id.login_register)
    TextView loginRegister;
    @Bind(R.id.widget_protocol_tv)
    TextView widgetProtocolTv;
    @Bind(R.id.login_mask)
    LinearLayout loginMask;
    private String email,pwd;
    private Gson gson;
    private String TAG="LoginActivity";
    private NiHaoHelper niHaoHelper;

    @Override
    public void initView() {
        super.initView();
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        toolbarBackTitle.setText(getString(R.string.title_login));
        loginMask.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction()==MotionEvent.ACTION_DOWN){
                    hideSoftKeyboard();
                }
                return true;
            }
        });

    }

    @Override
    public void initList() {
        super.initList();
        gson=new Gson();
        niHaoHelper=NiHaoHelper.getInstance();
    }


    @OnClick({R.id.toolbar_back_left, R.id.login_btn, R.id.login_forget_pwd, R.id.login_register, R.id.widget_protocol_tv, R.id.login_mask})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.toolbar_back_left:
                finish();
                break;
            case R.id.login_btn:
                pwd=loginPwdEdit.getText().toString();
                email=loginEmailEdit.getText().toString();
                Log.e(TAG,"请求了"+pwd+"---"+email);

                requestInfo();

                break;
            case R.id.login_forget_pwd:
                Intent intent1=new Intent(LoginActivity.this,ResetPwdActivity.class);

                startActivity(intent1);
                break;
            case R.id.login_register:
                Intent intent=new Intent(LoginActivity.this,RegisterActivity.class);
                startActivityForResult(intent, AppConstants.CONFIGURECODE);
                break;
            case R.id.widget_protocol_tv:
                Intent intent2=new Intent(LoginActivity.this,ProtocolActivity.class);
                startActivity(intent2);
                break;
            case R.id.login_mask:

                break;
        }
    }

    private void requestInfo() {
        showLoading();
        NiHaoOkgo.loginInfo(email,pwd)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<SimpleBaseEntity<String>>() {
                    @Override
                    public void call(SimpleBaseEntity<String> stringSimpleBaseEntity) {
                        dismissLoading();
                        if(stringSimpleBaseEntity.status){
                            AppInfo appInfo=new AppInfo(stringSimpleBaseEntity.token,stringSimpleBaseEntity.uid);
                            niHaoHelper.login(appInfo);
                            ((NiHaoApp)getApplication()).resumeData();
                            EventBus.getDefault().post(new MessageEvent("login"),"login");
                            showLoading();
                        }else{
                            Log.e(TAG,"登录错误"+stringSimpleBaseEntity.info);
                            showCanDialog(stringSimpleBaseEntity.info);
                        }
                    }
                });
    }

}
